//
//  VCOfClickLvXingDi.m
//  SeeTheWorld
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VCOfClickLvXingDi.h"
#import "HeaderOfClickLvXingDi.h"
#import "CellOfClickLvXingDi.h"
#import "PCH.pch"
#import "ModelOfClickLvXingDiHeader.h"
#import "ModelOfClickLvXingDi.h"
#import <UIImageView+WebCache.h>


@interface VCOfClickLvXingDi () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)ModelOfClickLvXingDiHeader *model;
@property (nonatomic, retain)UIButton *buttonOfLeft;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@end

@implementation VCOfClickLvXingDi

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_arrayOfModel release];
    [_buttonOfLeft release];
    [_model release];
    [_tableView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationController.navigationBar.hidden = YES;
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.arrayOfModel = [NSMutableArray array];
    
    NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@.json?page=1", self.id];
    [NetworkingHandler networkingWithURL:str completeHandle:^(NSData *data,id result) {
        
        self.model = [[ModelOfClickLvXingDiHeader alloc] init];
        self.model.image_url = [result objectForKey:@"image_url"];
        self.model.name_zh_cn = [result objectForKey:@"name_zh_cn"];
        self.model.name_en = [result objectForKey:@"name_en"];
        self.model.photos_count = [result objectForKey:@"photos_count"];
        self.model.attraction_trips_count = [result objectForKey:@"attraction_trips_count"];
        self.model.descrip = [result objectForKey:@"description"];
        
        for (NSDictionary *dic in [result objectForKey:@"attraction_trip_tags"]) {
            ModelOfClickLvXingDi *model = [[ModelOfClickLvXingDi alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayOfModel addObject:model];
            
            [model release];
        }
        
        [self.tableView reloadData];
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, self.view.frame.size.width, self.view.frame.size.height + 49) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434); ;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[HeaderOfClickLvXingDi class] forHeaderFooterViewReuseIdentifier:@"pool"];
    [self.tableView registerClass:[CellOfClickLvXingDi class] forCellReuseIdentifier:@"pool1"];
   
    self.tableView.bounces = NO;
    
    /** 主页navigation左侧button */
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(10, 20, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"fan"] forState:UIControlStateNormal];
    [self.view addSubview:self.buttonOfLeft];
}

- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return [HeaderOfClickLvXingDi heightForCell:self.model];
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    HeaderOfClickLvXingDi *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"pool"];
    [view passModel:self.model];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfClickLvXingDi *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
